package com.scoy.honeymei.fragment.mall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.TravelMeDetailActivity;
import com.scoy.honeymei.activity.mall.TripMeDetailActivity;
import com.scoy.honeymei.adapter.CouponListAdapter;
import com.scoy.honeymei.adapter.TravelMeAdapter;
import com.scoy.honeymei.adapter.TripMeAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.TravelOrderBean;
import com.scoy.honeymei.bean.TripOrderBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: CouponFragment$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/9/4/0004$ 上午 10:52$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/4/0004$ 上午 10:52$
 * @UpdateRemark: 更新内容
 */
public class TripMeFragment extends BaseFragment {


    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private int type;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<TripOrderBean> datalist;
    private TripMeAdapter tripMeAdapter;
    private int pageInt;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    public static TripMeFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        TripMeFragment fragment = new TripMeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();

        initNormal();

        httpTriplist(0);

        return view;
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();

        initRV();

        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                datalist.clear();
                httpTriplist(0);

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);
                httpTriplist(pageInt);

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initRV() {
        tripMeAdapter = new TripMeAdapter(mContext, datalist, nodataTv, type);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(tripMeAdapter);
        tripMeAdapter.setOnOneClick(new TripMeAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                TripOrderBean tripOrderBean = datalist.get(pos);
                int id = tripOrderBean.getId();
                Intent intent0 = new Intent(mContext, TripMeDetailActivity.class);
                intent0.putExtra("type", type);
                intent0.putExtra("tid", id);
                startActivity(intent0);
            }
        });

    }

    private void httpTriplist(int myPage) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(getActivity(), type == 1 ? MyUrl.TRIPEND_ORDER_LIST : MyUrl.TRIPPRE_ORDER_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<TripOrderBean> data = new Gson().fromJson(o, new TypeToken<List<TripOrderBean>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(data);
                tripMeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

}
